var db = require('../config/mongoose.js');
// var vfGroup = require('../entity/Group.js');

var GroupUser = db.model('GroupUser', {
    group_id: String,          //群 id
    user_name: String,
    creat_user: String,
    group_name: String,
    role:String, //角色 creater  admin  user
    status:{ // 默认参数
        type: Number,
        default: 1,
    }
});

// 加群
var create = function (group_id, user_name, group_name, callBack) {
    var groupUser = new GroupUser({
        group_id: group_id,
        group_name: group_name,
        user_name: user_name,
        role:"creater",
        creat_user: user_name,
        status:1,
    });
    //保存
    groupUser.save(function (err) {
        if (!err) {
            vfglobal.MyLog("加入群成功");
            if (callBack) callBack();
        }
    });
}

// 查询 加入过的所有群 以及群对应的 人
var findAllUser = function (res, req) {
    console.log("查找群用户")
    // console.log(req)
    var groupname = req.query.group_name;
    GroupUser.find({group_name:groupname}, function (err, data) {
        // console.log(arguments)
        if (data) { //说明 已经加入
            res.json({
                code: 1,
                message: "返回群用户",
                data: data
            });
        } else {
            res.json({
                code: 1,
                message: "没有找到",
                data: []
            });
        }
    });
}

// 查询 加入过的所有群 以及群对应的 人
var findAll = function (user_name, callBack) {
    GroupUser.find({user_name: user_name}, function (err, data) {
        callBack(data);
    });
}

// 查询 是否已经加入过群
var findAndInsert = function (group_id, user_name, group_name, callBack) {
    console.log("查找群用户")
    console.log(arguments)
    GroupUser.findOne({group_id: group_id, user_name: user_name }, function (err, data) {
        if (data) { //说明 已经加入
            vfglobal.MyLog("群：" + data.group_name + "已经加入过了");
            callBack();
        } else {
            //没有加入 现在加入
            var groupUser = new GroupUser({
                group_id: group_id,
                user_name: user_name,
                group_name: group_name
            });
            //保存
            groupUser.save(function (err) {
                if (!err) {
                    vfglobal.MyLog("加入群" + group_name + "成功");
                    if (callBack) callBack();
                }
            });
        }
    });
}

// 离开群
var leave = function (group_id, user_name, group_name, callBack) {
    GroupUser.findOne({group_id: group_id, user_name: user_name}, function (err, data) {
        if (data) { //说明 已经加入
            vfglobal.MyLog(user_name + "离开群：" + group_name);
            data.remove();
            callBack();
        } else {
            vfglobal.MyLog(user_name + " 没加过这个群。。。");
        }
    });
}


var remove = function (group_id,user_name,group_name) { 
    GroupUser.remove({ group_id: group_id,user_name:user_name,group_name:group_name })
}

var offLine = function () { 

}

var updateOne = function (query,content,callback) { 
    GroupUser.updateOne(query,content,callback)
}


var dissolve = function (group_id) { 
    GroupUser.remove({ group_id: group_id })
}

module.exports = {
    GroupUser,
    create,
    findAndInsert,
    leave,
    findAllUser,
    findAll,
    dissolve,
    offLine,
    remove,
    updateOne,
};